package com.speedchina.common.upload.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.speedchina.common.domain.ConversionResults;
import com.speedchina.common.domain.ExcelReadResult;
import com.speedchina.common.upload.base.BaseListener;
import com.speedchina.framework.model.R;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author xz
 * Data: 2021/7/7
 */
public class FileToConversionResult {

    public static ConversionResults fileToList(MultipartFile file, Class clazz,Integer sheetNo,Boolean headCheck) throws IOException {
        InputStream inputStream = MultipartFileToInputStreamUtils.excelFileToInputStream(file);
        if (inputStream != null){
            //默认是读取第一个shell
            BaseListener baseListener = new BaseListener(clazz);
            //读取数据
            EasyExcel.read(inputStream, clazz, baseListener).sheet(sheetNo).doReadSync();
            if (headCheck == true){
                //需要检验
                headCheck = baseListener.getTemplateCorrect();
            }else {
                //不需要校验
                headCheck = true;
            }
            if (headCheck){
                //模板正确执行
                List excelReadResultList = baseListener.getExcelReadResultList();
                //对读取到的list进行转换加校验
                return ListDataConverterUtils.listConverterCheck(excelReadResultList, clazz);
            }
        }
        return null;
    }
}
